package com.topboya.linker.ui.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.model.response.TaskProductResponse;
import com.topboya.linker.ui.activity.SeeSelectedProductActivity;
import com.topboya.linker.ui.activity.SeeTaskProductActivity;
import com.topboya.linker.ui.activity.xSelectFileActivity;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;

public class SeeTaskProductAdapter extends RecyclerView.Adapter<SeeTaskProductAdapter.ListItemViewHolder> {

    private static final String TAG = "SelectProductAdapter:";
    private SeeTaskProductActivity mContext;
    private ArrayList<TaskProductResponse.Data> mList;


    public SeeTaskProductAdapter(SeeTaskProductActivity mContext, ArrayList<TaskProductResponse.Data> list) {

        this.mContext = mContext;
        mList = list;
    }


    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_product_item, viewGroup, false);
        return new SeeTaskProductAdapter.ListItemViewHolder(itemView);
    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {

        //设置条目状态
        holder.tv_productName.setText(mList.get(i).getProductName());
        int isvisible =Integer.parseInt(mList.get(i).getFileNum()) > 0 ? View.VISIBLE : View.GONE;
        holder.img_data.setVisibility(isvisible);

        if (null !=mList.get(i).getFileNum() && Integer.parseInt(mList.get(i).getFileNum()) > 0){
            holder.tv_select_file_num.setVisibility(View.VISIBLE);
            holder.tv_select_file_num.setText(mList.get(i).getFileNum());
        }else {
            holder.tv_select_file_num.setVisibility(View.INVISIBLE);
        }

        //条目view的监听
        holder.itemView.setOnClickListener(v -> {

            itemClickListener.onClick(holder.itemView ,i);

        });

        holder.img_data.setOnClickListener(v1 -> {

            itemClickListener.onClick(holder.itemView ,i);

        });

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_productName;
        TextView tv_productcompany;
        TextView tv_select_file_num;
        ImageView img_data;

        ListItemViewHolder(View view) {
            super(view);
            this.tv_productName = view.findViewById(R.id.tv_productName);
            this.tv_productcompany = view.findViewById(R.id.tv_productcompany);
            this.tv_select_file_num = view.findViewById(R.id.tv_select_file_num);
            this.img_data = view.findViewById(R.id.img_data);
        }
    }

    private SeeTaskProductAdapter.OnItemClickListener itemClickListener;

    public void setOnItemClickListener(SeeTaskProductAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position );
    }



}
